package com.jcxy.controller;

import com.jcxy.pojo.User;
import com.jcxy.service.NotifyService;
import com.jcxy.util.PageBean;
import com.jcxy.util.ParamsDto;
import com.jcxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/notifyManage")
public class NotifyManageController {

    @Autowired
    private NotifyService notifyService;

    /**
     * 通过分页获得用户列表
     * @return
     */
    @RequestMapping("/getNotifyListByPage.do")
    @ResponseBody
    public ResponseData getNotifyListByPage(HttpServletRequest request){


        //将参数转换成合适的map集合
        Map dto = ParamsDto.getDto(request.getParameterMap());

        //调用业务逻辑层方法，查询角色列表
        PageBean pageBean = notifyService.queryListByPage(
                dto,//查询条件
                Integer.valueOf(dto.get("page").toString()),//当前页
                Integer.valueOf(dto.get("limit").toString()));//页容量

        return pageBean.getResponseData();
    }

    /**
     * 修改公告信息
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateNotifyById.do")
    public String updateNotifyById(HttpServletRequest request) throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        notifyService.updateNotifyById(dto);

        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "informationManage/notifyManage";
    }

    /**
     * 添加数据字典
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/addNotify.do")
    public String addNotify(HttpServletRequest request) throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        //获取登录user对象
        User user = (User) request.getSession().getAttribute("user");
        dto.put("user_id",user.getUser_id());
        notifyService.insertNotify(dto);

        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "informationManage/notifyManage";
    }

    /**
     * 通过通知id，删除通知信息
     * @param notify_id
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteNotifyById.do")
    @ResponseBody
    public ResponseData deleteNotifyById(Integer notify_id)throws Exception{

        notifyService.deleteNotifyById(notify_id);

        return ResponseData.ok();
    }
}
